package com.hll.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author: Mr.Hu
 * @create: 2021-03-13 22:21
 */
public class LoginInterceptor implements HandlerInterceptor {

  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws ServletException, IOException {
    // 如果是登陆页面则放行
    System.out.println("uri: " + request.getRequestURI());
    if (request.getRequestURI().contains("login")) {
      return true;
    }

    HttpSession session = request.getSession();

    // 如果用户已登陆也放行
    if (session.getAttribute("user") != null) {
      return true;
    }

    // 用户没有登陆跳转到登陆页面
    request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
    return false;
  }

  public void postHandle(
      HttpServletRequest httpServletRequest,
      HttpServletResponse httpServletResponse,
      Object o,
      ModelAndView modelAndView)
      throws Exception {}

  public void afterCompletion(
      HttpServletRequest httpServletRequest,
      HttpServletResponse httpServletResponse,
      Object o,
      Exception e)
      throws Exception {}
}
